var db = wx.cloud.database();
var userCollection = db.collection("usersDataList");
var recordCollection = db.collection("recordsData");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ["0"],
    useDateTime: ["0 ~ 1小时", "2 ~ 3小时", "4 ~ 5小时", "6小时以上"],
    insetTime: ["0 ~ 1小时", "2 ~ 3小时", "4 ~ 5小时", "6小时以上"],
    sitdown: [
      {
        name: "低头读写",
        check: false,
      },
      {
        name: "歪头读写",
        check: false,
      },
      {
        name: "握笔过低",
        check: false,
      },
      {
        name: "无",
        check: false,
      },
    ],
    glass: [
      {
        name: "戴镜松动",
        check: false,
      },
      {
        name: "日戴镜不足8小时",
        check: false,
      },
      {
        name: "无",
        check: false,
      },
    ],
    glassyes: [
      "未戴镜",
      "普通单光镜",
      "新乐学",
      "星趣控",
      "小乐圆",
      "控优点",
      "奥拉",
      "星乐视",
      "思问",
      "成长乐",
      "其它框架镜",
      "角膜塑形镜",
      "Misight",
      "蝶适",
      "捷安视",
      "安视美",
      "美乐沃",
      "其他接触镜",
    ],
    allergy: ["眼或鼻过敏", "无"],
    superimpose: [
      {
        name: "阿托品",
        check: false,
      },
      {
        name: "红光",
        check: false,
      },
      {
        name: "闭目晒眼",
        check: false,
      },
      {
        name: "远视镜",
        check: false,
      },
      {
        name: "调节力训练",
        check: false,
      },
      {
        name: "视距拉远装置",
        check: false,
      },
      {
        name: "雾视",
        check: false,
      },
      {
        name: "眼部理疗",
        check: false,
      },
      {
        name: "中草药",
        check: false,
      },
      {
        name: "无",
        check: false,
      },
    ],
    selfMag: {
      createDate: "",
      hospital: "",
      selfheight: "",
    },
    ygd: {
      hitomileft: "", //小瞳
      hitomiright: "",
      stleft: "", //散瞳
      stright: "",
      sgleft: "", //散光
      sgright: "",
      lyleft: "", //裸眼
      lyright: "",
      jzleft: "", //矫正
      jzright: "",
      yyleft: "", //眼压
      yyright: "",
    },
    yzd: {
      ALleft: "",
      ALright: "",
      K1R1left: "",
      K1R1right: "",
      K2R2left: "",
      K2R2right: "",
      ADACDleft: "",
      ADACDright: "",
      LTleft: "",
      LTright: "",
      gxsw: "",
    },
    tagSelect: {
      insetTime: null, //日户外时长
      glassyes: null, //戴镜
      useDateTime: null, //日近用眼时长
      allergy: null, //过敏反应
      sitdown: [], //坐姿不正确
      glass: [], //坐姿不正确
      superimpose: [], //叠加措施
      message: "",
    },
    gxswShow: false,
    gxswTag: [
      "蔡司IOL-Master",
      "尼德克NIDEK",
      "索维SW",
      "晶星Lenstar",
      "多美Tomey",
      "托普康Topcon",
      "蔡司colombo",
      "不清楚",
    ],
    currentDate: new Date().getTime(),
    showPicker: false,
    minDate: new Date(1950, 0, 1).getTime(), // 最小日期（2020-01-01）
    maxDate: new Date().getTime(), // 最大日期（2025-12-31）
    hasEmptyField: true,
    lastRecord: null, // 添加上次记录存储
    dsShow: false,
    dsTag: [
      "-0",
      "-1",
      "-2",
      "-3",
      "-4",
      "-5",
      "+0",
      "+1",
      "+2",
      "+3",
      "+4",
      "+5",
      "-6",
      "-7",
      "-8",
      "-9",
      "-10",
      "-11",
      "+6",
      "+7",
      "+8",
      "+9",
      "+10",
      "+11",
      "-12",
      "-13",
      "-14",
      "-15",
      "-16",
      "-17",
      "+12",
      "+13",
      "+14",
      "+15",
      "+16",
      "+17",
    ],
    dsDecimalTag: ["00", "25", "50", "75"],
    selectFlag: true,
    currentDsField: "",
    tempValue: "",
    currentTitle: "",
    visionTag: [
      "2.0/5.3",
      "1.5/5.2",
      "1.2/5.1",
      "1.0/5.0",
      "0.8/4.9",
      "0.6/4.8",
      "0.5/4.7",
      "0.4/4.6",
      "0.3/4.5",
      "0.25/4.4",
      "0.2/4.3",
      "0.15/4.2",
      "0.12/4.1",
      "0.1/4.0",
    ],
    visionShow: false,
    numberTag: [
      "10",
      "11",
      "12",
      "13",
      "14",
      "15",
      "16",
      "17",
      "18",
      "19",
      "20",
      "21",
      "22",
      "23",
      "24",
      "25",
      "26",
      "27",
      "28",
      "29",
      "30",
      "31",
      "32",
      "33",
      "34",
      "35",
      "36",
      "37",
      "38",
      "39",
      "04",
      "05",
      "06",
      "07",
      "08",
      "09",
    ],
    numberShow: false,
    alTag: [
      "16",
      "17",
      "18",
      "19",
      "20",
      "21",
      "22",
      "23",
      "24",
      "25",
      "26",
      "27",
      "28",
      "29",
      "30",
      "31",
      "32",
      "33",
      "34",
      "35",
      "36",
      "37",
      "38",
      "39",
    ],
    k1k2Tag: [
      "35",
      "36",
      "37",
      "38",
      "39",
      "40",
      "41",
      "42",
      "43",
      "44",
      "45",
      "46",
      "47",
      "48",
      "49",
      "50",
    ],
    adTag: ["1", "2", "3", "4", "5", "6"],
    decimalTag: [
      "00",
      "01",
      "02",
      "03",
      "04",
      "05",
      "06",
      "07",
      "08",
      "09",
      "10",
      "11",
      "12",
      "13",
      "14",
      "15",
      "16",
      "17",
      "18",
      "19",
      "20",
      "21",
      "22",
      "23",
      "24",
      "25",
      "26",
      "27",
      "28",
      "29",
      "30",
      "31",
      "32",
      "33",
      "34",
      "35",
      "36",
      "37",
      "38",
      "39",
      "40",
      "41",
      "42",
      "43",
      "44",
      "45",
      "46",
      "47",
      "48",
      "49",
      "50",
      "51",
      "52",
      "53",
      "54",
      "55",
      "56",
      "57",
      "58",
      "59",
      "60",
      "61",
      "62",
      "63",
      "64",
      "65",
      "66",
      "67",
      "68",
      "69",
      "70",
      "71",
      "72",
      "73",
      "74",
      "75",
      "76",
      "77",
      "78",
      "79",
      "80",
      "81",
      "82",
      "83",
      "84",
      "85",
      "86",
      "87",
      "88",
      "89",
      "90",
      "91",
      "92",
      "93",
      "94",
      "95",
      "96",
      "97",
      "98",
      "99",
    ],
    alShow: false,
    k1k2Show: false,
    adShow: false,
    currentType: "",
    hospitalShow: false,
    hospitalTag: [], // 改为空数组，从数据库获取
    isEditMode: false,
    editId: "",
    createDate: "",
    hospital: "",
    tags: { green: [], red: [] },
  },
  saveMsg() {
    // 校验必填项
    const selectedUserId = wx.getStorageSync("selectedUserId");
    const validationResult = this.validateRequiredFields();
    if (!validationResult.isValid) {
      wx.showToast({
        title: validationResult.message,
        icon: "none",
        duration: 2000,
      });
      return;
    }

    // 显示加载动画
    wx.showLoading({
      title: "正在保存...",
      mask: true,
    });

    // 准备保存的数据
    const saveData = {
      selectedUserId,
      selfMag: this.data.selfMag,
      ygd: this.data.ygd,
      yzd: this.data.yzd,
      tagSelect: this.data.tagSelect,
    };

    // 根据是否是编辑模式调用不同的云函数
    const cloudFunction = this.data.isEditMode ? "updateRecord" : "saveRecord";
    const cloudData = this.data.isEditMode
      ? { id: this.data.editId, recordData: saveData }
      : saveData;

    wx.cloud
      .callFunction({
        name: cloudFunction,
        data: cloudData,
      })
      .then((res) => {
        // 隐藏加载动画
        wx.hideLoading();

        if (res.result.success) {
          wx.showToast({
            title: this.data.isEditMode ? "更新成功" : "保存成功",
            icon: "success",
            duration: 1500,
            success: () => {
              // 延迟返回，让用户看到成功提示
              setTimeout(() => {
                if (this.data.isEditMode) {
                  // 清除编辑数据
                  wx.removeStorageSync("editData");
                  // 返回上一页
                  wx.navigateBack();
                } else {
                  wx.switchTab({
                    url: "/pages/home/home",
                  });
                }
              }, 1500);
            },
          });
        } else {
          wx.showToast({
            title:
              res.result.message ||
              (this.data.isEditMode ? "更新失败" : "保存失败"),
            icon: "error",
          });
        }
      })
      .catch((err) => {
        // 隐藏加载动画
        wx.hideLoading();

        console.error(err);
        wx.showToast({
          title: this.data.isEditMode ? "更新失败" : "保存失败",
          icon: "error",
        });
      });
  },
  validateRequiredFields() {
    // 基本信息校验
    if (!this.data.selfMag.createDate) {
      return {
        isValid: false,
        message: "请选择日期",
      };
    }
    if (!this.data.selfMag.hospital) {
      return {
        isValid: false,
        message: "请填写医院",
      };
    }

    // 验光单校验 - 必填项
    const requiredYgdFields = [
      "hitomiright",
      "hitomileft",
      "stright",
      "stleft",
    ];
    const missingYgdFields = requiredYgdFields.filter(
      (field) => !this.data.ygd[field]
    );
    if (missingYgdFields.length > 0) {
      const fieldNames = {
        hitomiright: "小瞳右眼",
        hitomileft: "小瞳左眼",
        stright: "散瞳右眼",
        stleft: "散瞳左眼",
      };
      const missingFieldNames = missingYgdFields
        .map((field) => fieldNames[field])
        .join("、");
      return {
        isValid: false,
        message: `请填写${missingFieldNames}`,
      };
    }

    // 眼轴单校验 - 必填项
    const requiredYzdFields = [
      "ALright",
      "ALleft",
      "K1R1right",
      "K1R1left",
      "K2R2right",
      "K2R2left",
    ];
    const missingYzdFields = requiredYzdFields.filter(
      (field) => !this.data.yzd[field]
    );
    if (missingYzdFields.length > 0) {
      const fieldNames = {
        ALright: "AL右眼",
        ALleft: "AL左眼",
        K1R1right: "K1R1右眼",
        K1R1left: "K1R1左眼",
        K2R2right: "K2R2右眼",
        K2R2left: "K2R2左眼",
      };
      const missingFieldNames = missingYzdFields
        .map((field) => fieldNames[field])
        .join("、");
      return {
        isValid: false,
        message: `请填写${missingFieldNames}`,
      };
    }

    // 负面因素校验
    if (!this.data.tagSelect.useDateTime) {
      return {
        isValid: false,
        message: "请选择日近用眼时长",
      };
    }
    if (this.data.tagSelect.sitdown.length === 0) {
      return {
        isValid: false,
        message: "请选择坐姿不正确选项",
      };
    }
    if (this.data.tagSelect.glass.length === 0) {
      return {
        isValid: false,
        message: "请选择戴镜不正确选项",
      };
    }
    if (!this.data.tagSelect.allergy) {
      return {
        isValid: false,
        message: "请选择过敏反应",
      };
    }

    // 防控措施校验
    if (!this.data.tagSelect.insetTime) {
      return {
        isValid: false,
        message: "请选择日户外时长",
      };
    }
    if (!this.data.tagSelect.glassyes) {
      return {
        isValid: false,
        message: "请选择戴镜类型",
      };
    }
    if (this.data.tagSelect.superimpose.length === 0) {
      return {
        isValid: false,
        message: "请选择叠加措施",
      };
    }

    return {
      isValid: true,
    };
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  changeTag(e) {
    const key = e.currentTarget.dataset.modoldata;
    this.setData({
      [`tagSelect.${key}`]: this.data[key][e.currentTarget.dataset.index],
    });
  },
  changeTagcheck(e) {
    const { modaldata: key, index } = e.currentTarget.dataset;
    const currentList = this.data[key]; // 获取选项数组
    const selectedItem = currentList[index].name; // 获取选中项的name
    let currentSelection = this.data.tagSelect[key] || []; // 当前选中项数组

    // 计算新的选中状态
    let newSelection;
    if (selectedItem === "无") {
      // 如果当前已选中"无"，则清空所有选项；否则只选中"无"
      newSelection = currentSelection.includes("无") ? [] : ["无"];
    } else {
      // 切换当前选项
      const isSelected = currentSelection.includes(selectedItem);
      newSelection = isSelected
        ? currentSelection.filter((item) => item !== selectedItem) // 取消选中
        : [...currentSelection.filter((item) => item !== "无"), selectedItem]; // 选中并移除"无"
    }

    // 更新选项的check状态
    const updatedList = currentList.map((item) => ({
      ...item,
      check: newSelection.includes(item.name),
    }));

    // 更新数据
    this.setData({
      [`${key}`]: updatedList,
      [`tagSelect.${key}`]: newSelection,
    });

    console.log("当前选中项:", newSelection);
  },
  messageChange(e) {
    const newValue = e.detail;
    this.setData({
      "tagSelect.message": newValue,
    });
  },
  heightChange(e) {
    const value = e.detail;
    this.setData({
      "selfMag.selfheight": value,
    });
  },
  hospitalChange(e) {
    this.setData({
      hospitalShow: true,
    });
  },
  selectHospitalTag(e) {
    const value = e.currentTarget.dataset.item;
    this.setData({
      hospitalShow: false,
      "selfMag.hospital": value,
    });
  },
  onHospitalCancel() {
    this.setData({
      hospitalShow: false,
    });
  },
  onHospitalConfirm() {
    const value = this.data.selfMag.hospital;
    if (!value) {
      wx.showToast({
        title: "请选择或输入医院名称",
        icon: "none",
      });
      return;
    }
    if (value.length > 4) {
      wx.showToast({
        title: "医院名称不能超过4个字",
        icon: "none",
      });
      return;
    }
    // 如果是自定义输入且不在预设标签中，则添加到数据库
    if (!this.data.hospitalTag.includes(value)) {
      this.addHospitalTag(value);
    }
    this.setData({
      hospitalShow: false,
    });
  },
  onHospitalInput(e) {
    const value = e.detail;
    this.setData({
      "selfMag.hospital": value,
    });
  },
  numberChange(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      numberShow: true,
      currentDsField: field,
      currentTitle: title,
    });
  },
  selectNumberTag(e) {
    const value = e.currentTarget.dataset.item;
    this.setData({
      numberShow: false,
      [`${this.data.currentDsField}`]: value,
    });
  },
  onNumberCancel() {
    this.setData({
      numberShow: false,
    });
  },
  onNumberConfirm() {
    this.setData({
      numberShow: false,
      [`${this.data.currentDsField}`]: "",
    });
  },
  toggleNumberShow() {
    this.setData({
      numberShow: !this.data.numberShow,
    });
  },
  jiajianChange(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      dsShow: true,
      currentDsField: field,
      currentTitle: title,
      selectFlag: true,
      tempValue: "",
    });
  },
  toggleDsShow() {
    this.setData({
      dsShow: !this.data.dsShow,
    });
  },
  onDsCancel() {
    this.setData({
      dsShow: false,
    });
  },
  onDsConfirm() {
    this.setData({
      dsShow: false,
      [this.data.currentDsField]: "",
    });
  },
  selectDsTag(e) {
    const value = e.currentTarget.dataset.item;
    if (this.data.selectFlag) {
      // 第一次选择
      this.setData({
        selectFlag: false,
        tempValue: value,
      });
    } else {
      // 第二次选择（小数点）
      const finalValue = this.data.tempValue + "." + value;
      this.setData({
        selectFlag: true,
        tempValue: "",
        dsShow: false,
        [`${this.data.currentDsField}`]: finalValue,
      });
    }
  },
  gxswChange() {
    this.setData({
      gxswShow: true,
    });
  },
  selectTag(e) {
    this.setData({
      gxswShow: false,
      ["yzd.gxsw"]: e.currentTarget.dataset.item,
    });
  },
  onCancel() {
    this.setData({
      gxswShow: false,
    });
  },
  onConfirm() {
    this.setData({
      gxswShow: false,
      ["yzd.gxsw"]: "",
    });
  },
  togglegxswShow() {
    this.setData({
      gxswShow: !this.data.gxswShow,
    });
  },
  showDateSelect() {
    this.setData({
      showPicker: true,
    });
  },
  // 确认选择
  onConfirmdate(event) {
    const selectedDate = new Date(event.detail);
    this.setData({
      showPicker: false,
      ["selfMag.createDate"]: `${selectedDate.getFullYear()}-${
        selectedDate.getMonth() + 1
      }-${selectedDate.getDate()}`,
    });
  },
  // 统一设置form字段的方法
  setFormData(key, value) {
    this.setData(
      {
        [`form.${key}`]: value.trim(),
      },
      this.checkFormEmpty
    );
  },
  // 检查是否有空值
  checkFormEmpty() {
    const { form } = this.data;
    const hasEmpty = Object.values(form).every((v) => !!v);
    this.setData({
      hasEmptyField: !hasEmpty,
    });
  },
  // 取消选择
  onCanceldate() {
    this.setData({
      showPicker: false,
    });
  },
  // 获取上次记录
  getLastRecord(e) {
    const module = e.currentTarget.dataset.module; // 获取当前模块
    const selectedUserId = wx.getStorageSync("selectedUserId");

    wx.showLoading({
      title: "加载中...",
    });

    recordCollection
      .where({
        _id: selectedUserId,
      })
      .orderBy("createTime", "desc")
      .limit(1)
      .get()
      .then((res) => {
        wx.hideLoading();
        console.log(res);

        if (res.data && res.data.length > 0) {
          const lastRecord = res.data[0];
          const recordsData = lastRecord.recordsdata;
          // 找到 selfMag.createDate 最大的一条
          let lastRecordData = null;
          if (recordsData && recordsData.length > 0) {
            lastRecordData = recordsData.reduce((latest, item) => {
              // 假设 selfMag.createDate 格式为 "YYYY-MM-DD"
              const latestDate = new Date(latest.selfMag.createDate);
              const itemDate = new Date(item.selfMag.createDate);
              return itemDate > latestDate ? item : latest;
            }, recordsData[0]);
          }

          this.setData({
            lastRecord: lastRecordData,
          });
          console.log(lastRecordData);
          // 根据模块更新数据
          if (module === "ygd" && lastRecordData) {
            this.setData({
              ygd: lastRecordData.ygd,
            });
          } else if (module === "yzd" && lastRecordData) {
            this.setData({
              yzd: lastRecordData.yzd,
            });
          }

          wx.showToast({
            title: "读取成功",
            icon: "success",
          });
        } else {
          wx.showToast({
            title: "没有历史记录",
            icon: "none",
          });
        }
      })
      .catch((err) => {
        wx.hideLoading();
        console.error("获取记录失败：", err);
        wx.showToast({
          title: "获取记录失败",
          icon: "none",
        });
      });
  },
  xiegangChange(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      visionShow: true,
      currentDsField: field,
      currentTitle: title,
    });
  },
  selectVisionTag(e) {
    const value = e.currentTarget.dataset.item;
    this.setData({
      visionShow: false,
      [`${this.data.currentDsField}`]: value,
    });
  },
  onVisionCancel() {
    this.setData({
      visionShow: false,
    });
  },
  onVisionConfirm() {
    this.setData({
      visionShow: false,
      [`${this.data.currentDsField}`]: "",
    });
  },
  toggleVisionShow() {
    this.setData({
      visionShow: !this.data.visionShow,
    });
  },
  ALChange(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      alShow: true,
      currentDsField: field,
      currentTitle: title,
      currentType: "al",
      selectFlag: true,
      tempValue: "",
    });
  },
  K1K2Change(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      k1k2Show: true,
      currentDsField: field,
      currentTitle: title,
      currentType: "k1k2",
      selectFlag: true,
      tempValue: "",
    });
  },
  ADChange(e) {
    const field = e.currentTarget.dataset.setmodol;
    const title = e.currentTarget.dataset.title;
    this.setData({
      adShow: true,
      currentDsField: field,
      currentTitle: title,
      currentType: "ad",
      selectFlag: true,
      tempValue: "",
    });
  },
  selectFirstTag(e) {
    const value = e.currentTarget.dataset.item;
    this.setData({
      selectFlag: false,
      tempValue: value,
    });
  },
  selectDecimalTag(e) {
    const decimal = e.currentTarget.dataset.item;
    const finalValue = this.data.tempValue + "." + decimal;
    this.setData({
      selectFlag: true,
      tempValue: "",
      alShow: false,
      k1k2Show: false,
      adShow: false,
      [`${this.data.currentDsField}`]: finalValue,
    });
  },
  onFirstCancel() {
    this.setData({
      alShow: false,
      k1k2Show: false,
      adShow: false,
      selectFlag: true,
      tempValue: "",
    });
  },
  onFirstConfirm() {
    this.setData({
      alShow: false,
      k1k2Show: false,
      adShow: false,
      selectFlag: true,
      tempValue: "",
      [`${this.data.currentDsField}`]: "",
    });
  },
  // 获取医院标签数据
  getHospitalTags() {
    const db = wx.cloud.database();
    db.collection("hospitalTags")
      .get()
      .then((res) => {
        console.log(res);

        if (res.data && res.data.length > 0) {
          this.setData({
            hospitalTag: res.data[0].taglist,
          });
        }
      })
      .catch((err) => {
        console.error("获取医院标签失败：", err);
        wx.showToast({
          title: "获取医院标签失败",
          icon: "none",
        });
      });
  },

  // 添加新的医院标签
  addHospitalTag(tag) {
    const db = wx.cloud.database();
    db.collection("hospitalTags")
      .get()
      .then((res) => {
        if (res.data && res.data.length > 0) {
          const docId = res.data[0]._id;
          const currentTags = res.data[0].tags;
          if (!currentTags.includes(tag)) {
            currentTags.push(tag);
            db.collection("hospitalTags")
              .doc(docId)
              .update({
                data: {
                  tags: currentTags,
                },
              })
              .then(() => {
                this.setData({
                  hospitalTag: currentTags,
                });
              });
          }
        }
      });
  },

  // 添加删除功能
  deleteRecord() {
    wx.showModal({
      title: "确认删除",
      content: "确定要删除这条记录吗？",
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: "正在删除...",
            mask: true,
          });

          wx.cloud
            .callFunction({
              name: "deleteRecord",
              data: {
                id: this.data.editId,
              },
            })
            .then((res) => {
              wx.hideLoading();
              if (res.result.success) {
                wx.showToast({
                  title: "删除成功",
                  icon: "success",
                  duration: 1500,
                  success: () => {
                    // 清除编辑数据
                    wx.removeStorageSync("editData");
                    // 返回上一页
                    setTimeout(() => {
                      wx.navigateBack();
                    }, 1500);
                  },
                });
              } else {
                wx.showToast({
                  title: res.result.message || "删除失败",
                  icon: "error",
                });
              }
            })
            .catch((err) => {
              wx.hideLoading();
              console.error("删除失败：", err);
              wx.showToast({
                title: "删除失败",
                icon: "error",
              });
            });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getHospitalTags();

    // 检查是否是编辑模式
    if (options.mode === "edit") {
      const editData = wx.getStorageSync("editData");
      if (editData) {
        const data = JSON.parse(editData);
        console.log(data);

        this.setData({
          isEditMode: true,
          editId: data.id,
          selfMag: {
            createDate: data.selfMag.createDate || "",
            hospital: data.selfMag.hospital || "",
            selfheight: data.selfMag.selfheight || "",
          },
          ygd: {
            hitomileft: data.ygd.hitomileft || "",
            hitomiright: data.ygd.hitomiright || "",
            jsleft: data.ygd.jsleft || "",
            jsright: data.ygd.jsright || "",
            jzleft: data.ygd.jzleft || "",
            jzleftStr: data.ygd.jzleftStr || "",
            jzright: data.ygd.jzright || "",
            jzrightStr: data.ygd.jzrightStr || "",
            lyleft: data.ygd.lyleft || "",
            lyleftStr: data.ygd.lyleftStr || "",
            lyright: data.ygd.lyright || "",
            lyrightStr: data.ygd.lyrightStr || "",
            sgleft: data.ygd.sgleft || "",
            sgright: data.ygd.sgright || "",
            stleft: data.ygd.stleft || "",
            stright: data.ygd.stright || "",
            yyleft: data.ygd.yyleft || "",
            yyright: data.ygd.yyright || "",
          },
          yzd: {
            ADACDleft: data.yzd.ADACDleft || "",
            ADACDright: data.yzd.ADACDright || "",
            ALleft: data.yzd.ALleft || "",
            ALright: data.yzd.ALright || "",
            K1R1left: data.yzd.K1R1left || "",
            K1R1right: data.yzd.K1R1right || "",
            K2R2left: data.yzd.K2R2left || "",
            K2R2right: data.yzd.K2R2right || "",
            LTleft: data.yzd.LTleft || "",
            LTright: data.yzd.LTright || "",
            gxsw: data.yzd.gxsw || "",
          },
          tagSelect: {
            allergy: data.tagSelect?.allergy || "",
            glass: data.tagSelect?.glass || [],
            glassyes: data.tagSelect?.glassyes || "",
            insetTime: data.tagSelect?.insetTime || "",
            message: data.tagSelect?.message || "",
            sitdown: data.tagSelect?.sitdown || [],
            superimpose: data.tagSelect?.superimpose || [],
            useDateTime: data.tagSelect?.useDateTime || "",
          },
        });

        // 更新选项的 check 状态
        if (data.tagSelect?.sitdown) {
          const updatedSitdown = this.data.sitdown.map((item) => ({
            ...item,
            check: data.tagSelect.sitdown.includes(item.name),
          }));
          this.setData({ sitdown: updatedSitdown });
        }

        if (data.tagSelect?.glass) {
          const updatedGlass = this.data.glass.map((item) => ({
            ...item,
            check: data.tagSelect.glass.includes(item.name),
          }));
          this.setData({ glass: updatedGlass });
        }

        if (data.tagSelect?.superimpose) {
          const updatedSuperimpose = this.data.superimpose.map((item) => ({
            ...item,
            check: data.tagSelect.superimpose.includes(item.name),
          }));
          this.setData({ superimpose: updatedSuperimpose });
        }

        console.log(this.data.tagSelect);
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
